import { permissionsRoute, openRouter } from "@/router/index";
//根据角色过滤侧边栏菜单菜单
export function filterMenu() {
    if (!localStorage.getItem('menu')) return []
    let menu = JSON.parse(localStorage.getItem('menu'))
    let permission = JSON.parse(JSON.stringify(permissionsRoute))
    permission.forEach((i) => {
        if (i.children?.length) {
            i.children.forEach((item) => {
                let single = menu.find((item2) => item2.name == item.meta.title)
                if (single) {
                    item.hidden = false
                } else {
                    item.hidden = true
                }
            })
            i['hidden'] = i.children.every(ite => ite.hidden === true);  //所有子菜单隐藏，父菜单隐藏
        }
    })
    return permission
}

export function isAllow(title) {
    let allow = [...filterMenu(), ...openRouter]
    let status = false
    allow.forEach((i) => {
        if (i.meta.title == title) status = true
        if (i.children?.length) {
            i.children.forEach((item) => {
                if (item.meta.title == title && !item.hidden) { //依据菜单名称匹配
                    status = true
                }
            })
        }
    })
    return status
}